package praca.impl;

import praca.framework.Sprite;
import praca.framework.SpriteBatcher;
import praca.framework.TextureAtlas;
import praca.framework.Vector;
import praca.interfejsy.Game;
import praca.interfejsy.Screen;
import praca.interfejsy.TouchEvent;

public class AnimationScreen extends Screen{

	private TextureAtlas penguinAtlas;
	private SpriteBatcher batcher;
	private Sprite[] penguins = new Sprite[7];
	private Sprite fishSprite;
	boolean isPlaying = false;
	private TouchEvent ev;
	float suma = 100;
	Vector touchVector = new Vector(0,0);
	Fish fish;
	
	public AnimationScreen(Game game) {
		super(game);
		fish = new Fish(700,50,50,50);
	}

	@Override
	public void update(float deltaTime) {
		suma = suma +deltaTime;
		
		
		if (!in.getTouchEvents().isEmpty()) {
			ev = in.getTouchEvents().remove(0);

			if (ev.getType() == TouchEvent.TOUCH_DOWN) {
				suma = 0;

				touchVector.setXY(ev.getX(), ev.getY());
				fish.setDistance(730,50, touchVector);
			}
		}
		
		g.enableDrawingStates();
		g.drawColor(1, 1, 1);
		
		fish.update(deltaTime);
		batcher.startBatch();
		
		
		batcher.drawSprite(fishSprite, 64, 64, fish.position.x, fish.position.y, fish.angle);
			playAnimation(deltaTime, 0.05f);	
		batcher.endBatch();
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		g.setTextureTransparentBackground();
		penguinAtlas = new TextureAtlas("rzucajacy.png", game);
		penguins[0] = new Sprite(penguinAtlas, 384,0,  512, 160);
		penguins[1] = new Sprite(penguinAtlas, 256,0,  384, 160);
		penguins[2] = new Sprite(penguinAtlas, 128,0,  256, 160);
		penguins[3] = new Sprite(penguinAtlas, 0,0,  128, 160);
		penguins[4] = new Sprite(penguinAtlas, 0,160,  128, 320);
		penguins[5] = new Sprite(penguinAtlas, 128,160,  256, 320);
		penguins[6] = new Sprite(penguinAtlas, 256,160,  384, 320);
		fishSprite = new Sprite(penguinAtlas, 384,160,  448, 224);
		batcher = new SpriteBatcher(penguinAtlas,game, 20);
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
	
	public void playAnimation(float deltaTime, float frameTime){
		suma = suma + deltaTime;
		
		int ramka = (int) (suma / frameTime);
		
		if(ramka >= penguins.length){
			batcher.drawSprite(penguins[0], 128, 160, 700,80);
			//suma =0; //jakby w kolko miala leciec
		}else{
			batcher.drawSprite(penguins[ramka], 128, 160, 700,80);
			if(ramka == 6){
				//start ryby
			}
		}	
	}

}
